package org.dragonnova.meetingapp.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.SystemClock;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 音频文件放在res/raw目录下面   test.wav
 * 调用:PromptUtil.poolPlay(SettingActivity.this, R.raw.test);
 */
public class PromptUtil {

    private static final int TYPE_PLAY_POOL = 1;
    private static final int TYPE_PLAY_MEDIA = 2;

    private  static SoundPool mSoundPool;
    private  static MediaPlayer mMediaPlayer;
    private  static ReentrantLock mLock = new ReentrantLock();
    private  static ExecutorService mExecutors = Executors.newCachedThreadPool();
    //此标记可以控制整个app的语音提示
    private final static boolean mAllowPrompt = true;

    public static void mediaPlay(Context context, int resId) {
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
        }
        if(mAllowPrompt){
            mExecutors.execute(new mediaRunnable(context, resId, TYPE_PLAY_MEDIA));
        }
    }

    public static void poolPlay(Context context, int resId) {
        /**
         * 初始化声音池
         * 参数1：在声音池中最大的数量
         * 参数2：流类型 默认使用AudioManager.STREAM_MUSIC
         * 参数3:质量  默认为0 这个值暂时没影响
         */
        if (mSoundPool == null) {
            mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        }
        if(mAllowPrompt){
            mExecutors.execute(new mediaRunnable(context, resId, TYPE_PLAY_POOL));
        }
    }

    private static class mediaRunnable implements Runnable {
        Context context;
        int resId;
        int type;

        public mediaRunnable(Context context, int resId, int playType) {
            this.context = context;
            this.resId = resId;
            this.type = playType;
        }

        @Override
        public void run() {
            switch (type) {
                case TYPE_PLAY_MEDIA:
                    mediaPlayFunc(context, resId);
                    break;
                case TYPE_PLAY_POOL:
                    poolPlayFunc(context, resId);
            }

        }
    }

    private static void mediaPlayFunc(Context context, int resId) {
        mLock.lock();
        try {
            AssetFileDescriptor fileDesc = context.getResources()
                    .openRawResourceFd(resId);
            if (fileDesc != null) {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(fileDesc.getFileDescriptor(), fileDesc.getStartOffset(),
                        fileDesc.getLength());
                fileDesc.close();
                mMediaPlayer.prepare();
                mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mMediaPlayer.reset();
                    }
                });
                mMediaPlayer.start();
                SystemClock.sleep(2000);
            }
        } catch (IOException e) {
            if (mLock.isLocked()) mLock.unlock();
            e.printStackTrace();
        } finally {
            mLock.unlock();
        }
    }

    private static void poolPlayFunc(Context context, int resId) {
        mLock.lock();
        try {

            /**
             * load加载声音
             * 参数1：上下文
             * 参数2：音频文件
             * 参数3：优先级 默认1 暂时没影响
             */
            final int soundId = mSoundPool.load(context, resId, 1);
            mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                    /**
                     * 参数1：加载返回的声音Id
                     * 参数2：左声道
                     * 参数3：右声道
                     * 参数4：优先级
                     * 参数5：是否循环播放 0：不循环 -1：循环
                     * 参数6：速率  0.5--2.0
                     */
                    soundPool.play(soundId, 1.0f, 0.5f, 1, 0, 1.0f);
                }
            });
            SystemClock.sleep(2000);
        } finally {
            mLock.unlock();
        }
    }

    public static void dismissMediaPlayer() {
        if (mSoundPool != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    /**
     * @Title: dismissSoundPool @Description: 释放播放池 @param 设定文件 @return void
     * 返回类型 @throws
     */
    public static void dismissSoundPool() {
        if (mSoundPool != null) {
            mSoundPool.release();
            mSoundPool = null;
        }
    }
}

